﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
            
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace bts_webapp
{
    public partial class SendMessage : BasePage
    {
        public void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = DbContext.VIBA_Users.Where(x => x.User_Status==1).ToList();
        }

        public void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblStatus.Text = string.Empty;
        }

        protected void btSend_Click(object sender, EventArgs e)
        {
            bool have = false;

            if (this.chkAll.Checked)
            {
                var users = this.DbContext.VIBA_Users.Where(x => x.User_Status == 1);
                if (users != null)
                {
                    foreach (var user in users)
                    {
                        var userIdTo = user.User_Id;
                        var file = new Models.VIBA_FILE
                        {
                            File_From = Common.CurrentUser.User_Id,
                            File_To = userIdTo,
                            File_Name = this.txtTitle.Text.Trim(),
                            File_Description = this.txtContent.Text.Trim(),
                            Status = 1,
                            SentDate = DateTime.Now
                        };
                        have = true;
                        this.DbContext.VIBA_FILEs.InsertOnSubmit(file);
                    }
                }
            }
            else
            {
                foreach (var item in this.RadGrid1.MasterTableView.Items)
                {
                    if (item is GridDataItem)
                    {
                        var dataItem = item as GridDataItem;
                        var check = (CheckBox)dataItem["chkCol"].FindControl("chkSelectedUser");
                        if (check.Checked)
                        {
                            var userIdTo = dataItem["UserId"].Text;
                            var file = new Models.VIBA_FILE
                            {
                                File_From = Common.CurrentUser.User_Id,
                                File_To = userIdTo,
                                File_Name = this.txtTitle.Text.Trim(),
                                File_Description = this.txtContent.Text.Trim(),
                                Status = 1,
                                SentDate = DateTime.Now
                            };
                            have = true;
                            this.DbContext.VIBA_FILEs.InsertOnSubmit(file);
                        }
                    }
                }
            }

            if (have)
            {
                this.DbContext.SubmitChanges();
            }

            this.lblStatus.Text = "Đã gởi message thành công.";
        }
    }
}